<template>
    <div class="article-list-body" :style="{ width: 1200 + 'px' }">
        <div class="article-panel">
      <div class="top-tab">
        <div
        >
          TA的博客
        </div>
      </div>
    </div>
      <div class="list" v-loading.fullscreen.lock="loading">
        <el-empty v-if="list.length<=0" description="暂无数据" />
        <div class="rows"  v-for="(item, index) in list" :key="index" @click="nav(item)">
          <div
            class="left"
            :style="{ width: item.image ? 'calc(100% - 140px)' : '100%' }"
          >
            <div class="title">{{ item.title }}</div>
            <div class="content">{{ item.content }}</div>
            <div class="bottom">
              <span class="user">{{ item.authorName }}</span>
              <span class="time"> {{
              item.created ? moment(item.created).format('YYYY-MM-DD HH:mm:ss') : ""
            }}</span>
            </div>
          </div>
          <img :src="item.image" alt="" style="width:100px;height:100px;margin-left:20px">
        </div>
      </div>
      <div style="display: flex; justify-content: center" v-if="list.length>0">
        <el-pagination
          size="large"
          @current-change="handlePageChange"
          :current-page="page"
          :page-size="pageSize"
          layout="prev, pager, next"
          :total="total"
          style="margin: 20px auto"
        ></el-pagination>
      </div>
    </div>
  </template>
  
  <script setup>
  import {
    ref,
    reactive,
    getCurrentInstance,
    defineProps,
    onMounted,
    watch,
  } from "vue";
  import { getMyArticles } from "@/api/article.js";
  import moment from 'moment'
  import { useRouter,useRoute} from "vue-router";
  import { useStore } from "vuex";
  const store =useStore()
  const router = useRouter();
  const route =useRoute();
  const list = ref([]);
  const total = ref(0);
  const page = ref(1);
  const pageSize = ref(10);
  const loading=ref(false)
  const getList = async () => {
    loading.value=true
    let res = await getMyArticles(store.state.userId, page.value);
    console.log(res,";;;")
    list.value = res.data.rows;
    total.value = res.data.total;
    setTimeout(() => {
        loading.value=false
    }, 1000);
  };
  onMounted(async () => {
    getList()
  });
  const getFormatTime = (arr) => {
    arr = arr.map((item) => {
      return (item = item < 10 ? "0" + item : item);
    });
    return (
      arr[0] +
      "-" +
      arr[1] +
      "-" +
      arr[2] +
      " " +
      arr[3] +
      ":" +
      arr[4] +
      ":" +
      arr[5]
    );
  };
  const handlePageChange = (e) => {
    page.value = e;
    getList();
  };
  const nav=(item)=>{
    store.commit("joinArticleId",item.id)
    store.commit("joinUserId",item.authorId)
    router.push({ path: '/detail', query: { type: 'other' }})
  }
  </script>
  
  <style scoped lang="scss">
  .article-list-body {
    .article-panel {
      background: #fff;
      .top-tab {
        display: flex;
        align-items: center;
        padding: 10px;
        font-size: 15px;
        border-bottom: 1px solid #ddd;
      }
    }
  }
  .list {
    .rows {
      display: flex;
      align-items: center;
      justify-content: space-between;
      background: white;
      padding: 16px 24px;
      height: 155px;
      box-sizing: border-box;
      border-bottom: 1px solid #ededed;
      cursor: pointer;
      .title {
        font-size: 22px;
      }
      .content {
        margin: 15px 0;
        color: #555666;
        overflow: hidden; //超出文本隐藏
        text-overflow: ellipsis; ///超出部分省略号显示
        display: -webkit-box; //弹性盒模型
        -webkit-box-orient: vertical; //上下垂直
        -webkit-line-clamp: 2; //自定义行数
      }
      .bottom {
        display: flex;
        justify-content: flex-end;
        color: #555666;
        gap: 20px;
      }
    }
  }
  </style>